package io.renren.modules.noc.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.noc.service.NocBeerTicketBusinessService;
import io.renren.modules.noc.service.NocBeerTicketMemberService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.noc.entity.NocBeerSeatEntity;
import io.renren.modules.noc.service.NocBeerSeatService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author whc
 * @email hucheng_86@126.com
 * @date 2019-07-11 17:58:09
 */
@RestController
@RequestMapping("noc/nocbeerseat")
public class NocBeerSeatController {
    @Autowired
    private NocBeerSeatService nocBeerSeatService;

    @Autowired
    private NocBeerTicketBusinessService nocBeerTicketBusinessService;

    @Autowired
    private NocBeerTicketMemberService nocBeerTicketMemberService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("noc:nocbeerseat:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = nocBeerSeatService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("noc:nocbeerseat:info")
    public R info(@PathVariable("id") Integer id){
        NocBeerSeatEntity nocBeerSeat = nocBeerSeatService.getById(id);

        return R.ok().put("nocBeerSeat", nocBeerSeat);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("noc:nocbeerseat:save")
    public R save(String[] ids,String businessId){
        for(String id:ids){
            if(StringUtils.isNotEmpty(id)) {
                NocBeerSeatEntity seat = new NocBeerSeatEntity();
                seat.setId(Integer.parseInt(id));
                seat.setBusinessId(Integer.parseInt(businessId));
                nocBeerSeatService.updateById(seat);
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("noc:nocbeerseat:update")
    public R update(@RequestBody NocBeerSeatEntity nocBeerSeat){
        ValidatorUtils.validateEntity(nocBeerSeat);
        nocBeerSeatService.updateById(nocBeerSeat);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("noc:nocbeerseat:delete")
    public R delete(@RequestBody Integer[] ids){
        nocBeerSeatService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 查询剩余票数
     * @param params
     * @return
     */
    @RequestMapping("/seatnum")
    public R seatnum(@RequestParam Map<String, Object> params){
        Map<String,Object> p = new HashMap<>();
        p.put("area",params.get("area"));
        p.put("table_row",params.get("table_row"));
        p.put("table_num",params.get("table_num"));
        p.put("valid_date",params.get("valid_date"));
        List<NocBeerSeatEntity> list = nocBeerSeatService.getSeatNum(p);
        if(list!=null&&list.size()>0){
            NocBeerSeatEntity beerSeatEntity = list.get(0);
            Map<String,Object> result = new HashMap<>();
            result.put("type",beerSeatEntity.getType());
            result.put("seatnum",beerSeatEntity.getLeftNum());
            result.put("seatnum_no",beerSeatEntity.getLeftNofpNum());
            return R.ok().put("result", result);
        }
        return R.error();
    }

    @RequestMapping("/getFullTable")
    public R getFullTable(@RequestParam Map<String, Object> params){
        Map<String,Object> p = new HashMap<>();
        p.put("area",params.get("area"));
        p.put("table_row",params.get("table_row"));
        p.put("table_num",params.get("table_num"));
        p.put("valid_date",params.get("valid_date"));
        List<NocBeerSeatEntity> list = nocBeerSeatService.getFullTable(p);
        return R.ok().put("list", list);
    }

    @RequestMapping("/getLeftNumTable")
    public R getLeftNumTable(@RequestParam Map<String, Object> params){
        Map<String,Object> p = new HashMap<>();
        p.put("area",params.get("area"));
        p.put("table_row",params.get("table_row"));
        p.put("table_num",params.get("table_num"));
        p.put("valid_date",params.get("valid_date"));
        List<NocBeerSeatEntity> list = nocBeerSeatService.getLeftNumTable(p);
        return R.ok().put("list", list);
    }


    //获取排号 - 展商
    @RequestMapping("getRow")
    public R getRow(@RequestParam Map<String, Object> params) {
        Map<String,Object> p = new HashMap<>();
        p.put("type","2");
        p.put("area",params.get("area"));
        p.put("valid_date",params.get("valid_date"));
        List<String> list = nocBeerSeatService.getRow(p);
        return R.ok().put("list", list);
    }

    //获取桌号 - 展商
    @RequestMapping("getNum")
    public R getNum(@RequestParam Map<String, Object> params) {
        Map<String,Object> p = new HashMap<>();
        p.put("type","2");
        p.put("area",params.get("area"));
        p.put("table_row",params.get("table_row"));
        p.put("valid_date",params.get("valid_date"));
        List<String> list = nocBeerSeatService.getNum(p);
        return R.ok().put("list", list);
    }

    //获取座位号 - 展商
    @RequestMapping("getSeat")
    public R getSeat(@RequestParam Map<String, Object> params) {
        Map<String,Object> p = new HashMap<>();
        p.put("type","2");
        p.put("area",params.get("area"));
        p.put("table_row",params.get("table_row"));
        p.put("table_num",params.get("table_num"));
        p.put("valid_date",params.get("valid_date"));
        List<NocBeerSeatEntity> list = nocBeerSeatService.getSeat(p);
        return R.ok().put("list", list);
    }

    @RequestMapping("remove")
    @ResponseBody
    public R remove(@RequestBody String[] ids) {
        for (String id : ids) {
            nocBeerSeatService.remove(id);
        }
        return R.ok();
    }

    @RequestMapping("count")
    @ResponseBody
    public R selectCount(@RequestParam Map<String, Object> params){
        String validDate = params.get("validDate").toString();
        Map<String,Object> map = new HashMap<>();
        map.put("validDate",validDate);
        Map<String,Object> seatMap =  nocBeerSeatService.selectCount(map);
        Integer a = Integer.parseInt(seatMap.get("bNotNull").toString());
        Integer b = Integer.parseInt(seatMap.get("bNull").toString());
        Integer c = Integer.parseInt(seatMap.get("mNotNull").toString());
        Integer d = Integer.parseInt(seatMap.get("mNull").toString());
        Integer e = a+b;
        Integer f = c+d;
        String seatNum = "观众座位："+a.toString()+"/"+e.toString()+"--------------"+"展商座位："+c.toString()+
                "/"+f.toString();
        Map<String,Object> lastMap = new HashMap<>();

        String business = "展商门票数量："+nocBeerTicketBusinessService.getAllCount(map);

        String member = "观众门票数量："+nocBeerTicketMemberService.getAllCount(map);

        lastMap.put("seatNum",seatNum);
        lastMap.put("business",business);
        lastMap.put("member",member);
        return R.ok(lastMap);
    }

}
